package com.np.xxpermissions1.permission;

import android.app.Activity;
import android.database.ContentObserver;
import android.os.Handler;
import android.provider.Settings;

import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
//import com.ultrasound.helper.Utils;

/**
 * 屏幕亮度监听
 */
public class BrightnessObserver extends ContentObserver {

    public static class Builder {
        static BrightnessObserver observer;
        static Builder instance = new Builder();

        public static Builder onCreate(Activity activity) {
            observer = new BrightnessObserver(activity);
            {
                activity.getContentResolver().registerContentObserver(Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS), true, observer);
                // 获取是否自动调整亮度
                try {
                    observer.m_automatic = (Settings.System.getInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
                } catch (Settings.SettingNotFoundException e) {
                    e.printStackTrace();
                }
                // 手动调整模式下记忆当前亮度值
                if (!observer.m_automatic) {
                    try {
                        observer.m_brightness = Settings.System.getInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
                    } catch (Settings.SettingNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
            return instance;
        }

        public void onPause() {
            if (observer.m_automatic) {
                // 若原来为自动调整模式,则恢复模式
                try {
                    if (Settings.System.getInt(observer.m_activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE) != Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
                        Settings.System.putInt(observer.m_activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
                    }
                } catch (Settings.SettingNotFoundException e) {
                    e.printStackTrace();
                }
            } else {
                // 若原来为手动调整模式,则恢复亮度值
                Settings.System.putInt(observer.m_activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, observer.m_brightness);
            }
        }

        public void onResume() {
            observer.setBrightness();
        }

        public void onDestroy() {
            this.onPause();
            observer.m_activity.getContentResolver().unregisterContentObserver(observer);
        }
    }

    private BrightnessObserver(Activity activity) {
        super(new Handler());
        m_activity = activity;
    }

    private void setBrightness() {
        if (XXPermissions.isGranted(m_activity, Permission.WRITE_SETTINGS)) {
            // 修改系统亮度调节模式为手动
            try {
                if (Settings.System.getInt(m_activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE) != Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL) {
                    Settings.System.putInt(m_activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
                }
            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
            }
            // 修改屏幕亮度
            //Utils.Display.setScreenBrightness(m_activity, Utils.SharedPreferences.getValue("brightness", 128) / 255.0f);
        }
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
//        try {
//            // 保存屏幕亮度
//            Utils.SharedPreferences.setValue("brightness", Settings.System.getInt(m_activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS));
//            // 修改屏幕亮度
//            this.setBrightness();
//        } catch (Settings.SettingNotFoundException e) {
//            e.printStackTrace();
//        }
    }

    private final Activity m_activity;
    private boolean m_automatic;
    private int m_brightness;
}
